package coin.yang.thread;

public class SellTicketDemo2 {

    public static void main(String[] args) {

        Ticket t = new Ticket();
        t.setCount(100);
        SellSystem ss = new SellSystem(t);
        Thread t1 = new Thread(ss);
        Thread t2 = new Thread(ss);
        Thread t3 = new Thread(ss);
        Thread t4 = new Thread(ss);

        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }

}

class SellSystem implements Runnable {

    private Ticket t;

    public SellSystem(Ticket t) {
        this.t = t;
    }

    @Override
    public void run() {

        while (true) {
            synchronized (this) {
                if (t.getCount() <= 0) break;
                t.setCount(t.getCount() - 1);
                System.out.println(t.getCount());
            }
        }

    }
}